#include "common.h"
#include "fs.h"
#include "memory.h"

#define DEFAULT_ENTRY ((void *)0x8048000)

extern void ramdisk_read(void *buf, off_t offset, size_t len);
extern size_t get_ramdisk_size();
extern void ramdisk_write(const void *buf, off_t offset, size_t len);

uintptr_t loader(_Protect *as, const char *filename) {
  int fd=fs_open(filename, 0, 0);
  size_t lens=fs_filesz(fd);
  void *va=DEFAULT_ENTRY;
  void *pa;
  int page_count=lens/PGSIZE+1;
  for(int i=0; i<page_count; i++) {
    pa=new_page();
    Log("Map va to pa: 0x%08x to 0x%08x", va, pa);
    _map(as, va, pa);
    fs_read(fd, pa, PGSIZE);
    va+=PGSIZE;
  }
  fs_close(fd);

  return (uintptr_t)DEFAULT_ENTRY;
}
